package it.controllocredito.backend;

import it.controllocredito.backend.phone.CustomPhoneStateListener;
import it.controllocredito.backend.phone.calllog.ManagerCallLogHandler;
import android.content.Context;
import android.telephony.TelephonyManager;
import android.util.Log;

public class Singleton
{
	
	private static final String			log_class_name	= "Singleton";
	
	private CustomPhoneStateListener	customPhoneStateListener;
	private TelephonyManager			telephonyManager;
	private ManagerCallLogHandler		managerCallLogHandler;
	
	// Private constructor prevents instantiation from other classes
	private Singleton()
	{
		Log.d(log_class_name, "- Singleton instance created");
	}
	
	/**
	 * SingletonHolder is loaded on the first execution of
	 * Singleton.getInstance() or the first access to SingletonHolder.INSTANCE,
	 * not before.
	 */
	private static class SingletonHolder
	{
		private static final Singleton	INSTANCE	= new Singleton();
	}
	
	public static Singleton getInstance()
	{
		Log.v(log_class_name, "Singleton getInstance");
		return SingletonHolder.INSTANCE;
	}
	
	// Global State Variables
	
	public CustomPhoneStateListener getCustomPhoneStateListener(Context context)
	{
		// lo ricrea ogni volta, bisogna creare un servizio
		if (this.customPhoneStateListener == null)
		{
			Log.d(log_class_name, "- Listener created");
			this.customPhoneStateListener = new CustomPhoneStateListener(
					context);
		}
		return this.customPhoneStateListener;
	}
	
	public TelephonyManager getTelephonyManager(Context context)
	{
		// lo ricrea ogni volta, bisogna creare un servizio
		if (this.telephonyManager == null)
		{
			Log.d(log_class_name, "- TelephonyManager created");
			this.telephonyManager = (TelephonyManager) context
					.getSystemService(Context.TELEPHONY_SERVICE);
		}
		return this.telephonyManager;
	}
	
	public ManagerCallLogHandler getManagerCallLogHandler(Context context)
	{
		// lo ricrea ogni volta, bisogna creare un servizio
		if (this.managerCallLogHandler == null)
		{
			Log.d(log_class_name, "- ManagerCallLogHandler created");
			this.managerCallLogHandler = new ManagerCallLogHandler(context);
		}
		return managerCallLogHandler;
	}
}